import requests  # 配置北向用户信息及北向地址nbi_name = "campus02@north.com"
from urllib3.exceptions import InsecureRequestWarning


class Get_Token:
    def __init__(self):
        self.token_id = ""
        self.nbi_name = "campusAc02@north.com"
        self.nbi_pwd = "Hn1sNQuBS@"
        self.host = "139.9.213.72"
        self.port = "18002"
        # 定义接口的 URI
        self.POST_TOKEN_URL = "/controller/v2/tokens"
        # 配置 URL 和 Headers
        self.post_token_url = "https://" + self.host + ":" + self.port + self.POST_TOKEN_URL
        self.headers_post = {'Content-Type': 'application/json', 'Accept': 'application/json'}
        self.token_id = self.get_token()

    def __int__(self, nbi_pwd, nbi_name, host, port, POST_TOKEN_URL, post_token_url, headers_post):
        pass

    def get_token(self):
        # 定义接口的 URI
        POST_TOKEN_URL = "/controller/v2/tokens"
        # 配置 URL 和 Headers
        post_token_url = "https://" + self.host + ":" + self.port + POST_TOKEN_URL
        headers_post = {'Content-Type': 'application/json', 'Accept': 'application/json'}

        requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
        # 发起请求，添加 Json 格式数据
        r = requests.post(post_token_url, headers=headers_post,json={"userName": self.nbi_name, "password": self.nbi_pwd}, verify=False)
        # 解析 token_id
        token_id = r.json()['data']['token_id']
        return token_id;


if __name__ == '__main__':
    connectToTheWbeService = Get_Token()
    print("1.【Get Token Id】")
    print("【post_token_url】：" + connectToTheWbeService.post_token_url)
    print("【token_id】：" + connectToTheWbeService.token_id)
